#include "utils.h"
#include <ctime>
#include <string>

namespace Utils
{
    std::string RandomString(size_t length)
    {
        static const char charset[] =
            "0123456789"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "abcdefghijklmnopqrstuvwxyz";
        const size_t max_index = (sizeof(charset) - 1);
        std::string str(length, 0);
        srand(static_cast<unsigned int>(time(nullptr))); // 初始化随机数种子
        for (size_t i = 0; i < length; ++i)
        {
            str[i] = charset[rand() % max_index];
        }
        return str;
    }

    void SleepMS(int msecs)
    {
        struct timespec t;
        t.tv_sec = msecs / 1000;
        t.tv_nsec = (msecs % 1000) * 1000000;
        nanosleep(&t, NULL);
    }

    uint64_t GetCurrentTimeMillis()
    {
        // 获取当前时间点
        auto now = std::chrono::system_clock::now();
        // 获取自1970年1月1日以来的微秒数
        auto duration = now.time_since_epoch();
        return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
    }

    std::string CombindString(const std::vector<std::string> &stringList)
    {
        std::stringstream ss;
        for (auto key : stringList)
        {
            ss << key << ", ";
        }
        return ss.str();
    }

}